package com.whw.fy.controller;

import com.whw.fy.UseToken;
import com.whw.fy.entity.Message;
import com.whw.fy.entity.TokenInfo;
import com.whw.fy.service.StaffService;
import com.whw.fy.service.UserService;
import com.whw.fy.utils.JwtUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

@Api(tags = "用户模块")
@RestController
@RequestMapping("/api/user")
public class UserController {

    @Resource
    private UserService userService;
    @Resource
    private StaffService staffService;

    @ApiOperation(value = "管理端登陆")
    @PostMapping("/admin/login")
    public Message adminLogin(int userId,String pwd){
        return userService.adminLogin(userId, pwd);
    }

    @ApiOperation(value = "小程序教师端账户登入")
    @GetMapping("/login")
    public Message miniProgramTeacherLogin(String jsCode){return userService.miniProgramTeacherLogin(jsCode); }

    @ApiOperation(value = "小程序用户绑定openId")
    @PostMapping("/binding")
    public Message bindingOpenIdByStaffId(String openId,int staffId){
        return userService.bindingOpenIdByStaffId(openId, staffId);
    }

    @ApiOperation(value = "小程序用户解除绑定openId")
    @PostMapping("/cancelbinding")
    public Message cancelBindingOpenIdByStaffId(int staffId){
        return userService.cancelBindingOpenIdByStaffId(staffId);
    }

    @ApiOperation(value = "根据token获取用户信息")
    @GetMapping("/info")
    public Message getUserInfoByToken(String token){
        TokenInfo tokenInfo = JwtUtil.getTokenInfo(token);
        return staffService.selectByPrimaryKey(tokenInfo.getStaffId());
    }

    @ApiOperation(value = "更改家长用户状态")
    @PutMapping("/parent/status")
    public Message updateParentStatus(int parentId, int status){
        return userService.updateParentStatus(parentId, status);
    }

    @ApiOperation("注销")
    @PostMapping("/logout")
    public Message logout(){
        return Message.success("注销成功");
    }

    @ApiOperation(value = "小程序家长端账户登入")
    @GetMapping("/login1")
    public Message miniProgramParentLogin(String jsCode){return userService.miniProgramParentLogin(jsCode); }

    @ApiOperation(value = "小程序账户登入")
    @GetMapping("/login2")
    public Message miniProgramLogin(String jsCode){return userService.miniProgramLogin(jsCode); }

    @ApiOperation(value = "小程序家长用户绑定openId")
    @PostMapping("/bind")
    public Message bindOpenIdByParentCall(String openId,String parentCall){
        return userService.bindOpenIdByParentCall(openId, parentCall);
    }

    @ApiOperation(value = "小程序家长用户解除绑定openId")
    @PostMapping("/cancelbind")
    public Message cancelBindOpenIdByParentCall(String parentCall){
        return userService.cancelBindOpenIdByParentCall(parentCall);
    }

}
